<?php
// +----------------------------------------------------------------------
// | SparkShop 坚持做优秀的商城系统
// +----------------------------------------------------------------------
// | Copyright (c) 2022~2099 http://sparkshop.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai  <1902822973@qq.com>
// +----------------------------------------------------------------------

namespace app\index\service;

use app\model\card\Card as CardModel;
use app\model\card\CardBig as CardBigModel;
use app\model\device\DeviceCard as DeviceCardModel;
use app\model\device\Device as DeviceModel;
use think\facade\Route;

class ApiService
{
    /**
     * binding
     * @param string $keys
     * @param string $device_num
     * @param string $user_num
     * @param string $ip
     * @return array|string
     */
    public function binding(string $keys, string $device_num, string $user_num, string $ip, string $desc ='')
    {
        // 该分类信息
        $cardModel = new CardModel();
        $deviceModel = new DeviceModel();
        $deviceCardModel = new DeviceCardModel();

        $card = $cardModel->findOne([ 'keys' => $keys ], '*')['data'];
        $times = date('Y-m-d H:i:s');
        $pre_keys = $pre_user_num = $usedeviceid = '';
        $usedeviceid = $usedevicecardid = 0;
        if (!$card) {
            return dataReturn(1 , '卡密不存在');
        }
        $card = $card->toArray();
        if($device_num){
            $usedevice = $deviceModel->findOne([ 'device_num' => $device_num ], '*','id desc')['data']->toArray();
            
            if($usedevice){
                $usedeviceid = $usedevice['id'];
                $usedevicecard = $deviceCardModel->findOne([ 'status'=>1,'device_id' => $usedevice['id'] ], '*','id desc')['data']->toArray();
                if($usedevicecard){
                    $usedevicecardid = $usedevicecard['id'];
                    $pre_keys = $usedevicecard['keys'];
                    $pre_user_num = $usedevicecard['pre_user_num'];
                }
                // if($usedevice['keys'] == $keys){
                //     return dataReturn(1 , '设备已绑定此秘钥');
                // }else{
                //     return dataReturn(1 , '设备已绑定');
                // }
            }
        }
        
        // 判断秘钥是否可绑定
        if($card['is_del']==0 && $card['status']==1){
            if($card['use_device_nums'] >= $card['device_nums']){
                return dataReturn(1 , '绑定数量已满，请联系管理员');
            }
        }else{
            return dataReturn(1 , '秘钥不可用，请联系管理员');
        }
        // 插入数据
        $add = $deviceCardModel->insert(array('device_id'=>$usedeviceid,'keys'=>$keys,'device_num'=>$device_num,'user_num'=>$user_num,'pre_user_num'=>$pre_user_num,'pre_keys'=>$pre_keys,'desc'=>$desc,'ip'=>$ip,'create_time'=>$times));
        if($usedeviceid > 0){
            $deviceModel->where([ 'id' => $usedeviceid ])->update(['keys'=>$keys,'user_num'=>$user_num,'status'=>2,'update_time'=>$times]);
        }
        
        if($usedevicecardid > 0){
            $deviceCardModel->where([ 'id' => $usedevicecardid ])->update(['status'=>2]);
            $cardModel->where([ 'id' => $card['id'] ])->update(['use_device_nums'=>$card['use_device_nums'],'is_sale'=>1,'update_time'=>$times]);
        }else{
            $cardModel->where([ 'id' => $card['id'] ])->update(['use_device_nums'=>$card['use_device_nums']+1,'is_sale'=>1,'update_time'=>$times]);
        }
        // $cateInfo = $cardModel->findOne(['id' => $cateId], 'id,name,pid')['data'];
       
        return dataReturn(0, 'success', [
            'keys' => $keys,
            'card_id' => $card['id'],
            'device_num' => $device_num,
            'user_num' => $user_num,
            'ip' => $ip,
        ]);
    }
    /**
     * unbinding
     * @param string $keys
     * @param string $unkeys
     * @param string $ip
     * @return array|string
     */
    public function unbinding(string $keys, string $unkeys, string $ip, string $desc ='')
    {
        // 该分类信息
        $cardModel = new CardModel();
        $deviceModel = new DeviceModel();
        $deviceCardModel = new DeviceCardModel();

        $card = $cardModel->findOne([ 'keys' => $keys ], '*')['data'];
        $times = date('Y-m-d H:i:s');
        $pre_keys = $pre_user_num = $usedeviceid = '';
        $usedeviceid = $usedevicecardid = 0;
        if ($card) {
            if($card['randoms'] != $unkeys){
                return dataReturn(1 , '操作密钥不正确');
            }
            if($card['use_device_nums'] > 0){
                $cardModel->where(array('id'=>$card['id']))->dec('use_device_nums',1)->update();
                $deviceCardModel->where(array('keys'=>$keys,'status'=>1))->update(['status'=>2]);
                $deviceModel->where(array('keys'=>$keys))->update(['keys'=>'']);
            }else{
                $deviceCardModel->where(array('keys'=>$keys,'status'=>1))->update(['status'=>2]);
                $deviceModel->where(array('keys'=>$keys))->update(['keys'=>'']);
            }
        }else{
            $deviceCardModel->where(array('keys'=>$keys,'status'=>1))->update(['status'=>2]);
            $deviceModel->where(array('keys'=>$keys))->update(['keys'=>'']);
        }
        return dataReturn(0, 'success', [
            'keys' => $keys,
            'ip' => $ip,
        ]);
    }
    
}